package dao;

import entity.Staffs;
import utli.DBCon;

import java.util.ArrayList;
import java.util.List;

public class StaffsDao {
    //添加
    public static int add(Staffs sta) {
        int result = 0;
        result = DBCon.ex("insert into staffs(name,age,phone,pos,add_time,update_time)" +
                " values('"+sta.getName()+"',"+sta.getAge()+"," +
                "'"+sta.getPhone()+"','"+sta.getPos()+"','"+sta.getAdd_time()+"'," +
                "'"+sta.getUpdate_time()+"')");
        return result;
    }

    //修改
    public static int update(Staffs sta) {
        int result = 0;
        result = DBCon.ex("update staffs set name='"+sta.getName()+"' where id="+sta.getId()+"");
        return result;
    }

    //删除
    public static int del(int index) {
        int result = 0;
        result = DBCon.ex("delete from staffs where id="+index+"");
        return result;
    }

    //查询
    public static List<Staffs> query() {
        List<Staffs> list = new ArrayList<>();

        list = DBCon.query("select id,name,age,phone,pos,add_time,update_time from staffs;");

        return list;
    }

    //分页查询
    public static List<Staffs> queryFy(int index,int page) {
        List<Staffs> list = DBCon.query("select\n" +
                "\tid,\n" +
                "\tname,\n" +
                "\tage,\n" +
                "\tphone,\n" +
                "\tpos,\n" +
                "\tadd_time,\n" +
                "\tupdate_time \n" +
                "from\n" +
                "\tstaffs limit "+index+" OFFSET "+page+";");
        return list;
    }

    //
    public static List<Staffs> queryId(int id) {
        List<Staffs> list = DBCon.query("select\n" +
                "\tid,\n" +
                "\tname,\n" +
                "\tage,\n" +
                "\tphone,\n" +
                "\tpos,\n" +
                "\tadd_time,\n" +
                "\tupdate_time \n" +
                "from\n" +
                "\tstaffs where id="+id+"");
        return list;
    }

    public static void main(String[] args) {
        Staffs sta = new Staffs();
        sta.setName("zl");
        sta.setAge(16);
        sta.setPhone("16779335176");
        sta.setPos("包工头");
        sta.setAdd_time("2022-01-03");
        sta.setUpdate_time("2024-03-06");
        add(sta);
    }
}
